/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.contract.bean;

import com.yunhoo.contract.db.impl.ConExecImpl;
import com.yunhoo.contract.db.impl.ContractImpl;
import com.yunhoo.contract.db.impl.ContractStatsImpl;
import com.yunhoo.contract.model.ConExecMaster;
import com.yunhoo.contract.model.ContractStats;
import com.yunhoo.contract.model.MainInformation;
import com.yunhoo.contract.model.PayInformation;
import com.yunhoo.contract.util.DocumentQueryConditionBeanContract;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.ExportExcel;
import com.yunhoo.sys.util.common.LoginParam;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "contractStatsBean")
@SessionScoped
public class ContractStatsBean implements Serializable {

    private List<ContractStats> statsli = null;
    private ContractStats totalRow = null;
    private DocumentQueryConditionBeanContract queryCondition = new DocumentQueryConditionBeanContract();//动态查询条件
    private boolean querybut = true;
    //
    private MainInformation mainInfo = null;
    private List<PayInformation> payInfoli = null;
    private List<ConExecMaster> detailli = null;//执行单 执行内容

    public void query() {
        initList();
    }

    public void queryCancel() {
        queryCondition = new DocumentQueryConditionBeanContract();
        initList();
    }

    public void hiddenbut() {
        querybut = false;
    }

    public void showbut() {
        querybut = true;
    }

    public void showRowDetail(String contractNumber) {
        System.out.println(" * * * " + contractNumber + "  * * * ");
        initDetailInfo(contractNumber);
    }

    private void initDetailInfo(String number) {
        mainInfo = ContractImpl.queryMainInfoByNumber(number);
        payInfoli = ContractImpl.queryPayInformationList(number);
        detailli = ConExecImpl.queryExecStats(number);
    }

    private void initList() {
        statsli = ContractStatsImpl.queryStatsList(conditionMap());
        initTotalRow();
    }

    private void initTotalRow() {
        totalRow = new ContractStats();
        if (statsli == null || statsli.isEmpty()) return;
        BigDecimal target = BigDecimal.valueOf(0),
                quality_margin = BigDecimal.valueOf(0),
                penalty = BigDecimal.valueOf(0),
                planTotal = BigDecimal.valueOf(0),
                execTotal = BigDecimal.valueOf(0),
                payTotal = BigDecimal.valueOf(0);
        for (ContractStats row : statsli) {
            target = target.add(row.getMainInfo().getTarget());
            quality_margin = quality_margin.add(row.getMainInfo().getQuality_margin());
            penalty = penalty.add(row.getMainInfo().getPenalty());
            planTotal = planTotal.add(row.getPlanTotal());
            execTotal = execTotal.add(row.getExecTotal());
            payTotal = payTotal.add(row.getPayTotal());
        }
        totalRow.getMainInfo().setTarget(target);
        totalRow.getMainInfo().setQuality_margin(quality_margin);
        totalRow.getMainInfo().setPenalty(penalty);
        totalRow.setPlanTotal(planTotal);
        totalRow.setExecTotal(execTotal);
        totalRow.setPayTotal(payTotal);
    }

    private Map conditionMap() {
        Map params = new HashMap();
        params.put("number", queryCondition.getNumber());
        params.put("name", queryCondition.getContractType().getName());
        params.put("cname", queryCondition.getCname());
        params.put("code", queryCondition.getContractType().getCode());
        params.put("orderdatestart", queryCondition.getOrderdatestart());
        params.put("orderdateend", queryCondition.getOrderdateend());
        params.put("address", queryCondition.getAddress());
        params.put("startstart", queryCondition.getStartstart());
        params.put("startend", queryCondition.getStartend());
        params.put("deadlinestart", queryCondition.getDeadlinestart());
        params.put("deadlineend", queryCondition.getDeadlineend());
        params.put("nameA", queryCondition.getNameA());
        params.put("nameAstand", queryCondition.getNameAstand());
        params.put("partnerName", queryCondition.getPartner().getName());
        params.put("legalperson", queryCondition.getPartner().getLegalPerson());
        params.put("targetstart", queryCondition.getTargetstart());
        params.put("targetend", queryCondition.getTargetend());
        params.put("quality_marginstart", queryCondition.getQuality_marginstart());
        params.put("quality_marginend", queryCondition.getQuality_marginend());
        params.put("penaltystart", queryCondition.getPenaltystart());
        params.put("penaltyend", queryCondition.getPenaltyend());
        params.put("other", queryCondition.getOther());
        params.put("pay", queryCondition.getPay());
        params.put("warrantystart", queryCondition.getWarrantystart());
        params.put("warrantyend", queryCondition.getWarrantyend());
        params.put("payPlanDateStart", queryCondition.getDate1_start());
        params.put("payPlanDateEnd", queryCondition.getDate1_end());
        params.put("isEffect", queryCondition.getIsEffect());
        return params;
    }

    public void exeportExcel() {
        if (statsli == null) return;
        String[] header = {"编号", "名称", "合同类别", "地点", "签订日期", "开始日期", "截止日期", "质保日期", "甲方", "甲方法人", "乙方", "乙方法人", "标的", "质保金", "违约金", "计划总金额", "执行总金额", "付款总金额", "其他"};
        Object[][] data = new Object[statsli.size() + 2][header.length];
        data[0] = header;
        for (int i = 0; i < statsli.size(); i++) {
            Object[] row = statsli.get(i).toArray();
            data[i + 1] = row;
        }
        Object[] footer = {"合计", "", "", "", "", "", "", "", "", "", "", "",
            totalRow.getMainInfo().getTarget(),//"标的", 
            totalRow.getMainInfo().getQuality_margin(),// "质保金",
            totalRow.getMainInfo().getPenalty(),//"违约金",
            totalRow.getPlanTotal(),//"计划总金额",
            totalRow.getExecTotal(),//"执行总金额",
            totalRow.getPayTotal(),//"付款总金额",
            ""};
        data[statsli.size() + 1] = footer;
        ExportExcel.exportExcel(data, "合同列表");
    }

    public List<ContractStats> getStatsli() {
        return statsli;
    }

    public void setStatsli(List<ContractStats> statsli) {
        this.statsli = statsli;
    }

    public DocumentQueryConditionBeanContract getQueryCondition() {
        return queryCondition;
    }

    public void setQueryCondition(DocumentQueryConditionBeanContract queryCondition) {
        this.queryCondition = queryCondition;
    }

    public boolean isQuerybut() {
        return querybut;
    }

    public void setQuerybut(boolean querybut) {
        this.querybut = querybut;
    }

    public MainInformation getMainInfo() {
        return mainInfo;
    }

    public void setMainInfo(MainInformation mainInfo) {
        this.mainInfo = mainInfo;
    }

    public List<PayInformation> getPayInfoli() {
        return payInfoli;
    }

    public void setPayInfoli(List<PayInformation> payInfoli) {
        this.payInfoli = payInfoli;
    }

    public List<ConExecMaster> getDetailli() {
        return detailli;
    }

    public void setDetailli(List<ConExecMaster> detailli) {
        this.detailli = detailli;
    }

    public ContractStats getTotalRow() {
        return totalRow;
    }

    public void setTotalRow(ContractStats totalRow) {
        this.totalRow = totalRow;
    }
}
